<?php
/**
 * User: dimka3210
 * Date: 19.11.13
 * Time: 20:20
 */

class FileStorage implements IStorage
{

    public function getRows($limit, $offset, $only_active = true)
    {
        $result = array();
        $files = $this->getFiles();
        foreach ($files as $file) {
            $cur_item = $this->fileToArray($file, false);
            if (count($cur_item))
                array_push($result, $cur_item);
        }

        return $result;
    }

    public function findRowById($id)
    {
        $file = CNF::$params['storage']['file_path'] . '/' . $id;
        return $this->fileToArray($file);
    }

    public function saveRow($data)
    {
        if (isset($data['id'])) {
            $id = $data['id'];
            unset($data['id']);

            $data['modify_date'] = date('Y-m-d H:i:s');

            $text = '';
            foreach ($data as $k => $v) {
                $text .= '=====' . $k . '=====';
                $text .= $v . (preg_match("#\\n$#", $v) ? '' : PHP_EOL);
                $text .= '=====' . $k . '/end=====' . PHP_EOL;
            }

            $path = CNF::$params['storage']['file_path'] . '/' . $id;
            file_put_contents($path, $text);
        } else {
            $data['create_date'] = $data['modify_date'] = date('Y-m-d H:i:s');

            $text = '';
            foreach ($data as $k => $v) {
                $text .= '=====' . $k . '=====';
                $text .= $v . (preg_match("#\\n$#", $v) ? '' : PHP_EOL);
                $text .= '=====' . $k . '/end=====' . PHP_EOL;
            }

            $path = CNF::$params['storage']['file_path'] . '/' . ($this->getLastId() + 1);
            file_put_contents($path, $text);
        }
    }

    public function deleteRow($id)
    {
        $file = CNF::$params['storage']['file_path'] . '/' . $id;
        unlink($file);
        return true;
    }

    private function getFiles()
    {
        $path = CNF::$params['storage']['file_path'];
        $files = glob($path . '/*');

        return $files;
    }

    private function fileToArray($filename, $only_active = true)
    {
        $cur_item = array();
        $_file = file_get_contents($filename);

        $cur_item['id'] = basename($filename);

        preg_match('#={5}is_active={5}(.*)={5}is_active/end={5}#s', $_file, $is_active);
        $cur_item['is_active'] = intval(isset($is_active[1]) ? $is_active[1] : '');

        if (!$cur_item['is_active'] && $only_active)
            return array();

        preg_match('#={5}title={5}(.*)={5}title/end={5}#s', $_file, $title);
        $cur_item['title'] = (isset($title[1])) ? $title[1] : '';

        preg_match('#={5}text={5}(.*)={5}text/end={5}#s', $_file, $text);
        $cur_item['text'] = (isset($text[1])) ? $text[1] : '';

        preg_match('#={5}create_date={5}(.*)={5}create_date/end={5}#s', $_file, $create_date);
        $cur_item['create_date'] = (isset($create_date[1])) ? $create_date[1] : '';

        preg_match('#={5}modify_date={5}(.*)={5}modify_date/end={5}#s', $_file, $modify_date);
        $cur_item['modify_date'] = (isset($modify_date[1])) ? $modify_date[1] : '';

        return $cur_item;
    }

    private function getLastId()
    {
        $filelist = $this->getFiles();
        if (!count($filelist))
            return 0;

        $ids = array();
        foreach ($filelist as $file) {
            array_push($ids, basename($file));
        }

        return max($ids);
    }
}